import 'package:eplorex/dao/group_dao.dart';
import 'package:eplorex/pages/message/message_conversation.dart';
import 'package:eplorex/providers/user_context.dart';
import 'package:flutter/material.dart';

import '../../../models/im_common_model.dart';

class GroupCreatePage extends StatefulWidget {
  const GroupCreatePage({super.key});

  @override
  State<GroupCreatePage> createState() => _GroupCreatePageState();
}

class _GroupCreatePageState extends State<GroupCreatePage> {
  final _formKey = GlobalKey<FormState>();
  late String _groupName = '';
  late String _groupDesc = '';
  final TextStyle _defaultTextStyle = TextStyle(fontSize: 18);

  _createGroup() {
    if (_formKey.currentState!.validate()) {
      _formKey.currentState!.save();
      GroupDao.createGroup(_groupName, _groupDesc, '', true).then((value) => {
            Navigator.of(context).pushReplacement(MaterialPageRoute(
                builder: (_) => ConversationDetailPage(
                    _groupName,
                    0,
                    convType: ConvType.group.value,
                    UserContext.cxt()!.uid,
                    (value as Map<String, dynamic>)['partnerId'] as int)))
          });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("创建群"),
      ),
      body: Container(
        margin: EdgeInsets.symmetric(horizontal: 20),
        child: Column(
          children: [
            Form(
                key: _formKey,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      '群名称',
                      style: _defaultTextStyle,
                    ),
                    TextFormField(
                      decoration: const InputDecoration(
                          filled: true,
                          hintText: '群名称',
                          border: OutlineInputBorder(
                            borderSide: BorderSide(color: Colors.blue),
                            borderRadius: BorderRadius.all(Radius.circular(10)),
                          )),
                      validator: (value) {
                        if (value == null || value.isEmpty) {
                          return '请输入群名称';
                        }
                        return null;
                      },
                      onSaved: (value) => _groupName = value!,
                    ),
                    Card(
                      margin: EdgeInsets.symmetric(vertical: 20),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text('群公告信息', style: _defaultTextStyle),
                              Switch(value: true, onChanged: (value) {})
                            ],
                          ),
                          Container(
                            height: 160,
                            child: TextFormField(
                              minLines: 5,
                              maxLines: 5,
                              decoration: const InputDecoration(
                                  filled: true,
                                  hintText: '用一段话介绍群聊吧！',
                                  border: OutlineInputBorder(
                                    borderSide: BorderSide(color: Colors.blue),
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(10)),
                                  )),
                              validator: (value) {
                                if (value == null || value.isEmpty) {
                                  return '添加一下群介绍吧';
                                }
                                return null;
                              },
                              onSaved: (value) => _groupDesc = value!,
                            ),
                          )
                        ],
                      ),
                    ),
                    Text('群类型', style: _defaultTextStyle),
                    Card(
                      child: Container(
                        height: 100,
                      ),
                    ),
                    Row(
                      children: [
                        Text('地点:', style: _defaultTextStyle),
                        IconButton(
                            onPressed: () {},
                            icon: Icon(Icons.location_on_outlined))
                      ],
                    )
                  ],
                )),
            FilledButton(
              onPressed: _createGroup,
              child: Text('现在创建'),
              style: ButtonStyle(
                minimumSize: MaterialStateProperty.all(
                    Size(MediaQuery.sizeOf(context).width / 8 * 7, 40)),
              ),
            )
          ],
        ),
      ),
    );
  }
}
